const app = getApp()
const WXAPI = require("../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgHttp: app.globalData.imgHttp, //图片前缀 
    changeIndex: 0,
    tabIndex: 0,
    laxLs: [],
    shareImg: '',
    assetId: '',
    id: '',
    info: {},
    dd: [],
    dataOptions: [],
    information: {},
    useIsshow: false,
    propShow: false, //剩余次数弹窗
    projectnoLs: [], //剩余项目(已用完)
    noList: '',
    moreShow: false,
    zzcShow: false,
    getfocus: false,
    getfocusks: false,
    cardTypeShow: '', //2：计次卡弹窗自动弹出   4：计时卡弹窗自动弹出
    pathType: '', //进来的路径 payTransit
    useShow: false, //用卡弹窗
    projectLs: [], //计次卡核销项目
    resultObject: {},
    jfpropShow: false, //多项计费卡核销弹窗
    returnShow: false, //体验卡退卡弹窗
    applyShow: false, //适用门店弹窗
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pathType = options.pathType
    console.log('pathType', pathType)
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom,
      pathType: pathType,
    })
    this.data.assetId = options.assetId
    this.data.id = options.id
    // this.data.assetId = '1472039146557071362'
    // this.data.id = '1473208063686086658'
    this.data.cardTypeShow = options.cardTypeShow
    if (this.data.cardTypeShow == 2 || this.data.cardTypeShow == 4) {
      this.getDetail()
      setTimeout(() => {
        this.payCard()
      }, 500)
    }
    // this.getDetail()
    // this.getCommission()
    wx.removeStorageSync('renovate')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    console.log(this.data.cardTypeShow)
    if (this.data.cardTypeShow != 'undefined') {
      this.getDetail()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(this.data.info.referrerId)
    return {
      title: '成为' + this.data.info.shopName + '会员，点击加入...',
      path: `/userPageB/pages/userStore/cardDetail/cardDetail?assetId=` + this.data.assetId + '&referrerId=' + this.data.info.referrerId + '&typePath=share',
    }
  },
  jumpAppeal(){ //跳转申诉
    wx.navigateTo({
      url: './appeal/appeal?merchantId=' + this.data.info.merchantId + '&returnId=' + this.data.info.returnId + '&assetId=' + this.data.assetId + '&id=' + this.data.id
    })
  },
  jumpAppealFeedback(){ //申诉反馈
    wx.navigateTo({
      url: './appealFeedback/appealFeedback?businessId=' + this.data.info.returnId,
    })
  },
  activationCard(){ //激活卡片
    var that = this
    var data = {
      id: this.data.id
    }
    wx.showLoading({
      title:'激活中',
      mask: true
    })
    WXAPI.cardActivation(data, (res) => {
      if (res.code === 200) {
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        wx.setStorageSync('renovate', '1')// 缓存是否刷新卡列表状态 1： 刷新  2：不刷新
        setTimeout(() => {
          that.getDetail()
          wx.hideLoading()
        }, 600)
      } else {
        wx.hideLoading()
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  getmoreShow() { //点击更多
    var moreShow = !this.data.moreShow
    this.setData({
      moreShow
    })
  },
  closeMoretc() {
    this.setData({
      moreShow: false
    })
  },
  cardInfo(e) { //查看购卡信息
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../buyCardinfo/buyCardinfo?id=' + id,
    })
  },
  //跳转店铺
  handleShop(e) { //isDeactivate
    var id = e.currentTarget.dataset.id
    var data = {
      shopId: id
    }
    WXAPI.isDeactivate(data, (res) => {
      if (res.code == 200) {
        wx.navigateTo({
          url: '/userPageB/pages/userStore/storesDetails/storesDetails?shopId=' + id,
        })
      }
    })
  },
  getDetail() {
    wx.showLoading({
      title: '加载中',
    })
    // var id = this.data.assetId
    // var url = `/app/v2/customer/asset/card/shareholder/detail/${id}`
    var data = {
      id: this.data.id,
      assetId: this.data.assetId
    }
    WXAPI.cardDetail(data, (res) => {
      this.data.info = res.result
      if (this.data.info.appCustomerCardNumberDetailDTOList.length != 0) {
        //处理计次卡剩余项目数组
        // this.handledtoLs()
      } else {
        this.data.noList = 1
      }
      this.setData({
        info: res.result
      })
      // setTimeout(()=>{
      //   this.getDividend()
      // },600)
      wx.hideLoading()
    })
  },
  returnCard() { //体验卡退卡
    this.setData({
      returnShow: true
    })
  },
  sumitReturn(e) { //确认退卡
    var that = this
    let cardId = e.currentTarget.dataset.info.id
    var data = {
      id: cardId
    }
    //experienceReturnCard :  旧退卡接口(体验卡)
    //allReturnCard 新退卡接口
    wx.showLoading({
      mask: true
    })
    WXAPI.allReturnCard(data, (res) => {
      if (res.code === 200) {
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        wx.setStorageSync('renovate', '1')// 缓存是否刷新卡列表状态 1： 刷新  2：不刷新
        setTimeout(() => {
          that.getDetail()
          that.setData({
            returnShow: false
          })
          wx.hideLoading()
        }, 600)
      } else {
        wx.hideLoading()
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  closeReturn() {
    this.setData({
      returnShow: false
    })
  },
  sumitRefund(e) { //确认已退款
    var that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      content: '您确定商家已经给您退款了吗？',
      cancelColor: '#999999',
      confirmColor: '#FF5800',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var data = {
            customerCardId: id
          }
          WXAPI.confirmRefund(data, (res) => {
            const data = res.result
            if (res.code === 200) {
              wx.showToast({
                title: '确认退款成功',
                icon: 'none'
              })
              setTimeout(() => {
                that.getDetail()
              }, 600)
            } else {
              wx.showToast({
                title: res.message,
                icon: 'none'
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  handledtoLs() {
    let ls = this.data.info.appCustomerCardNumberDetailDTOList
    console.log(ls)
    this.data.projectLs = ls.filter((item) => {
      return item.projectNumberUsable != 0
    })
    this.data.projectnoLs = ls.filter((item) => {
      return item.projectNumberUsable == 0
    })
    this.setData({
      projectLs: this.data.projectLs,
      projectnoLs: this.data.projectnoLs
    })
  },
  // 查看预付码
  watchCode(){
    wx.navigateTo({
      url: './prepaidCode/prepaidCode?customerCardId=' + this.data.info.id,
    })
  },
  payCard(e) { //用会员卡
    let info = e.currentTarget.dataset.info
    if(info.cardType==7){
      wx.navigateTo({
        url: '/userPageA/pages/welfareCard/payCard/payCard',
      })
      return
    }
    if (info.cardType==2||info.cardType==4||info.cardType==9) { //计次卡 || 课时卡
      wx.navigateTo({
        url: './prepaidCode/prepaidCode?customerCardId=' + this.data.info.id,
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    var data = {
      customerCardId: info.id
    }
    WXAPI.verificationCardInfo(data, (res) => {
      if (res.code == 200) {
        this.data.resultObject = res.result
        if (res.result.cardType == 5) {
          this.data.projectLs = res.result.appCustomerCardExperienceDetailDTOList
        } else {
          this.data.projectLs = res.result.appCustomerCardNumberDetailDTOList
        }
        wx.hideLoading()
        if (res.result.qrCode != '') { //多项目计费卡
          wx.navigateTo({
            url: './prepaidCode/prepaidCode?customerCardId=' + this.data.info.id,
          })
          // this.setData({
          //   jfpropShow: true,
          //   projectLs: this.data.projectLs,
          //   resultObject: this.data.resultObject
          // })
        } else {
          this.setData({
            useShow: true,
            projectLs: this.data.projectLs,
            resultObject: this.data.resultObject
          })
        }
      }
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 6000)
  },
  //适用店铺
  payApply() {
    this.setData({
      applyShow: true
    })
  },
  chose() {
    this.setData({
      applyShow: false
    })
  },
  onScuess(e) { //用卡成功
    let detail = JSON.stringify(e.detail)
    wx.redirectTo({
      url: '../../../../userPageB/pages/buyCardpuch/puchSuccess/puchSuccess?detail=' + detail + '&cardType=' + this.data.info.cardType,
    })
  },
  propFalse() { //关闭多项目计费卡核销弹窗
    this.setData({
      jfpropShow: false
    })
  },
  back() {
    wx.navigateBack({
      delta: 1,
    })
  },
  handleChose() {
    wx.switchTab({
      url: '/pages/userMarket/userMarket',
    })
  },
  //查看合同
  handleContract(e) {
    wx.showLoading({
      title: '加载中',
      mask: true,
      icon:'none'
    })
    let url = e.currentTarget.dataset.contract
    var index = url.lastIndexOf("\.");
    var type = url.substring(index + 1, url.length);
    wx.downloadFile({
      // url: app.globalData.contractHttp,
      url: this.data.imgHttp + url,
      header: {},
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          fileType: type,
          success: function (res) {
            console.log('打开文档成功')
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
        wx.hideLoading()
      },
      fail: function (res) {
        console.log('文件下载失败');
        wx.hideLoading()
      },
      complete: function (res) {
        wx.hideLoading()
      },
    })

  },
  jumpDetail() { //股东卡详情
    console.log(this.data.info)
    if (this.data.info.cardType == 7) {
      wx.navigateTo({
        url: '/userPageA/pages/welfareCard/cardDetail/cardDetail?assetId=' + this.data.assetId,
      })
    } else {
      wx.navigateTo({
        url: '/ecCanvas/pages/myShareholderCard/card_detail/card_detail?assetId=' + this.data.assetId + '&merchantId=' + this.data.info.merchantId,
      })
    }
  },
  jumpDetailed(e) { //明细
    let id = e.currentTarget.dataset.id
    let info = e.currentTarget.dataset.info
    if (info.cardType == 2 || info.cardType == 5) {
      wx.navigateTo({
        url: '/ecCanvas/pages/myShareholderCard/jcBalanceDetails/jcBalanceDetails?id=' + id + '&cardType=' + info.cardType + '&noList=' + this.data.noList,
      })
    } else if (info.cardType == 4||info.cardType == 9) {
      wx.navigateTo({
        url: '/ecCanvas/pages/myShareholderCard/ksBalanceDetails/ksBalanceDetails?id=' + id + '&cardType=' + info.cardType,
      })
    } else {
      wx.navigateTo({
        url: '/ecCanvas/pages/myShareholderCard/balanceDetails/balanceDetails?id=' + id,
      })
    }
  },
  jumpBalance(e) { //查看余额
    // let id = e.currentTarget.dataset.id
    let balanceMoney = this.data.info.balanceMoney
    let rechargeMoney = this.data.info.rechargeMoney
    let giveMoney = this.data.info.giveMoney
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/balance/balance?balanceMoney=' + balanceMoney + '&rechargeMoney=' + rechargeMoney + '&giveMoney=' + giveMoney,
    })
  },
  profit(e) { //分红收益
    let info = e.currentTarget.dataset.info
    let assetId = info.assetId
    let id = info.id
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/myShareholderCard?assetId=' + assetId + '&id=' + id,
    })
  },
  jumpHonor(e) { //荣誉分享
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/honorShare/honorShare?id=' + id
    })
  },
  moreClick(){ //更多诚信卡
    wx.reLaunch({
      url: '/pages/userMarket/userMarket',
    })
  },
  junpShop(e) { //跳转店铺
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/userPageB/pages/userStore/storesDetails/storesDetails?shopId=' + id,
    })
  },
  //充值
  handletopup(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/userPageA/pages/topup/topup?customerCardId=' + id,
    })
  },
  //立即使用
  handleConsumption(e) {
    var aid = e.currentTarget.dataset.aid;
    var mid = e.currentTarget.dataset.mid;
    let info = e.currentTarget.dataset.info;
    if (info.cardType == 1 || info.cardType == 3 || info.cardType == 6 || info.cardType == 7) {
      // return
      this.getcardUse(info.id)
    } else if (info.cardType == 2 || info.cardType == 4 || info.cardType == 5) {
      this.getcardUse(info.id)
    } else {
      wx.redirectTo({
        url: '/pages/pay/pay?assetId=' + aid + '&merchantId=' + mid,
      })
    }
  },
  //买单
  jumpPay(e) {
    let info = e.currentTarget.dataset.info;
    var aid = e.currentTarget.dataset.aid;
    var mid = e.currentTarget.dataset.mid;
    this.closePay()
    if (info.cardType == 2 || info.cardType == 4) {
      this.getcardUse(info.id)
    } else {
      wx.navigateTo({
        url: '/pages/pay/pay?assetId=' + aid + '&merchantId=' + mid,
      })
    }
  },
  callphone(e) { //联系商家
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  getcardUse(e) { //立即使用
    wx.showLoading({
      title: '加载中',
    })
    let customerCardId = e
    var data = {
      customerCardId: customerCardId
    }
    WXAPI.customerAssetInfo(data, (res) => {
      if (res.code == 200) {
        wx.hideLoading()
        this.setData({
          information: res.result,
          useIsshow: true
        })
      }
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 6000)
  },
  closeUse() {
    this.setData({
      useIsshow: false
    })
  },
})